package com.hw.one.dam2.dao;

import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.hw.one.dam2.param.CityAirParam;

@Repository(value = "cityAirDao")
public class CityAirDao implements ICityAirDao {

	@Autowired
	private MongoTemplate mongoTemplateStandardSiteData;

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findCityHourDataByRegionCodeAndTime(CityAirParam cityAirParam) {
		Calendar calendar = Calendar.getInstance();

		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
				.lte(cityAirParam.getEndTime());
		criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

		// 创建查询对象
		Query query = new Query(criteria);

		// 增加排序功能
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

		// 查询数据 小时数据
		List<Map> cityHourAirList = mongoTemplateStandardSiteData.find(query, Map.class,
				"T_DATA_CITY_HOUR_S_COLLECT-" + calendar.get(Calendar.YEAR));

		return cityHourAirList;
	}

	// 城市、区县 日数据
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findCityDayDataByRegionCodeAndTime(CityAirParam cityAirParam) {
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
				.lte(cityAirParam.getEndTime());
		criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

		// 创建查询对象
		Query query = new Query(criteria);

		// 增加排序功能
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

		// 查询数据 日数据
		List<Map> cityDayAirList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_CITY_DAY_S_COLLECT");

		return cityDayAirList;
	}

	// 城市、区县 月数据
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findCityMonthByRegionCodeAndTime(CityAirParam cityAirParam) {
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
				.lte(cityAirParam.getEndTime());
		criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

		// 创建查询对象
		Query query = new Query(criteria);

		// 增加排序功能
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

		// 查询数据 月数据
		List<Map> cityMonthAirList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_CITY_MONTH_S_TOTAL");

		return cityMonthAirList;
	}

	// 城市、区县 年数据
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findCityYearByRegionCodeAndTime(CityAirParam cityAirParam) {
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
				.lte(cityAirParam.getEndTime());
		criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

		// 创建查询对象
		Query query = new Query(criteria);

		// 增加排序功能
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

		// 查询数据 年数据
		List<Map> cityYearAirList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_CITY_YEAR_S_TOTAL");

		return cityYearAirList;
	}


	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findLastCityMonthTotalByRegionCodeAndTimeList(CityAirParam cityAirParam) {
		Criteria criteria = Criteria.where("REGIONCODE").in(cityAirParam.getRegionCodes());
		criteria.and("MONITORTIME").is(cityAirParam.getEndTime());
		// 创建查询对象
		Query query = new Query(criteria);
		// 查询数据
		List<Map> resultMap = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_CITY_MONTH_S_TOTAL");
		return resultMap;
	}


	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findCityNewHourDataByRegionCodes(List<String> regionCodeList) {

		Calendar calendar = Calendar.getInstance();
		String tableName = "T_DATA_CITY_HOUR_S_COLLECT-" + calendar.get(Calendar.YEAR);

		Map cityAQIInfo = mongoTemplateStandardSiteData.findOne(new Query(), Map.class, tableName);
		// 得到最新时间
		Object timeObj = cityAQIInfo.get("MONITORTIME");

		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").is(timeObj);
		criteria.and("REGIONCODE").in(regionCodeList);
		// 创建查询对象
		Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "AQI"));

		List<Map> cityNewDataList = mongoTemplateStandardSiteData.find(query, Map.class, tableName);

		return cityNewDataList;
	}


}
